import {
  createRouter,
  createWebHashHistory
} from 'vue-router';
import store from '../store';

export const routes = [{
    path: '/',
    redirect: '/goods/front.vue',
    component: () => import('../views/main.vue'),
    children: [{
        path: '/banner',
        component: () => import('../views/banner/index.vue'),
        meta: {
          label: '轮播管理',
          icon: 'PictureFilled'
        },
        children: [{
            path: '/banner/list',
            component: () => import('../views/banner/list.vue'),
            meta: {
              label: "轮播列表",
            },
          },
          {
            path: '/banner/pub',
            component: () => import('../views/banner/pub.vue'),
            meta: {
              label: "轮播发布",
            },
          },
          {
            path: '/banner/edit',
            component: () => import('../views/banner/edit.vue'),
            meta: {
              label: "轮播编辑",
            },
          }
        ]
      },
      {
        path: '/goods',
        component: () => import('../views/goods/index.vue'),
        meta: {
          label: "产品管理",
          icon: "Present",
        },
        children: [{
            path: '/goods/front.vue',
            component: () => import('../views/goods/list-front.vue'),
            meta: {
              label: "商品列表(前端筛选)",
            },
          },
          {
            path: '/goods/back.vue',
            component: () => import('../views/goods/list-back-end.vue'),
            meta: {
              label: "商品列表(后端筛选)",
            },
          }
        ]
      },
      {
        path: '/manager',
        component: () => import('../views/manager/index.vue'),
        meta: {
          label: '系统设置',
          icon: 'Setting'
        },
        children: [{
            path: '/manager/role-sim',
            component: () => import('../views/manager/role-sim.vue'),
            meta: {
              label: '角色管理(简单)'
            }
          },
          {
            path: '/manager/role-dif',
            component: () => import('../views/manager/role-dif.vue'),
            meta: {
              label: '角色管理(完整)'
            }
          },
          {
            path: "/manager/user",
            component: () => import("../views/manager/user.vue"),
            meta: {
              label: "用户管理",
            },
          },
        ]
      },
      {
        path: '/excel',
        component: () => import('../views/excel/index.vue'),
        meta: {
          label: 'excel表格',
          icon: 'Files'
        },
        children: [{
            path: '/excel/inport',
            component: () => import('../views/excel/inport.vue'),
            meta: {
              label: '导入excel'
            }
          },
          {
            path: '/excel/export',
            component: () => import('../views/excel/export.vue'),
            meta: {
              label: '导出excel'
            }
          }
        ]
      },
      {
        path: '/rich',
        component: () => import('../views/rich-edit.vue'),
        meta: {
          label: '富文本编辑器',
          icon: 'Edit'
        }
      },
      {
        path: '/echats',
        component: () => import('../views/echarts/index.vue'),
        meta: {
          label: '数据可视化',
          icon: 'Histogram'
        }
      },
      {
        path: '/map',
        component: () => import('../views/map.vue'),
        meta: {
          label: '百度地图',
          icon: 'Promotion'
        }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/nopermission',
    component: () => import('../views/nopermission.vue')
  }

]


const router = createRouter({
  history: createWebHashHistory(),
  routes
})

//设置路由白名单
let info = localStorage.getItem('vue-admin-userinfo');
let whiteList = ['/', '/nopermission']

const getWhiteList = (arr) => {
  arr.forEach(item => {
    whiteList.push(item.path)
    if (item.children) {
      getWhiteList(item.children)
    }
  });
}
if (info) {
  let {
    roleMenu
  } = JSON.parse(info)
  getWhiteList(roleMenu)
}
console.log("路由白名单", whiteList);

//全局路由守卫
router.beforeEach((to, from, next) => {
  if (to.path.indexOf('/login') == -1) {
    if (store.state.user.userInfo) {
      console.log(whiteList, to.path)
      if (whiteList.includes(to.path)) {
        next()
      } else {
        next('/nopermission')
      }
    } else {
      next('/login')
    }
  } else {
    next()
  }
})


export default router;